/*
 * fml_pwm.c
 *
 *  Created on: 2025年4月10日
 *      Author: 22332
 */
#include <stdio.h>
#include <stdlib.h>
#include "cola_init.h"
#include "cola_device.h"
#include "cola_os.h"
#include "fml_pwm.h"
#include "shell.h"

extern void logWrite(const char *fmt, ...);

static cola_device_t *g_pDevpwm = NULL;
static uint8_t g_iHostPWMValue = 240;					//主机发过来的PWM值
static uint8_t g_iSetPWMValue = 240;					//上层设置的PWM值

void fml_set_host_pwm_value(int value)
{
	g_iHostPWMValue = value;
}

int fml_get_host_pwm_value(void)
{
	return g_iHostPWMValue;
}

int fml_get_bsp_pwm_value(void)
{
	return g_iSetPWMValue;
}

void fml_pwm_init(void)
{
	g_pDevpwm = cola_device_find("DEVICE_PWM");
    if(g_pDevpwm)
        cola_device_open(g_pDevpwm, 0);
}

void fml_pwm_change_duty(uint8_t duty)
{
	g_iSetPWMValue = duty;
	cola_device_write(g_pDevpwm,duty,0,0);
}

void fml_pwm_deinit(void)
{
    if(g_pDevpwm)
    {
    	g_pDevpwm = NULL;
        cola_device_close(g_pDevpwm);
    }
}

#if 1
int fml_pwm(int argc, char *argv[])
{
	if (argc < 3)
	{
		logWrite("para length must greater than 3\r\n");
		return 0;
	}
	int mode 		= atoi(argv[1]);
	int value 		= atoi(argv[2]);
	if(mode == 0)
	{
		logWrite("pwm_value=%d\r\n",value);
		fml_pwm_change_duty(value);
	}
	else if(mode == 1)
	{
		logWrite("g_iHostPWMValue=%d\r\n",fml_get_host_pwm_value());
	}
	else if(mode == 2)
	{
		fml_set_host_pwm_value(value);
		fml_pwm_change_duty(value);
	}
	return 0;
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN),fml_pwm, fml_pwm, fml_pwm fml_pwm);
#endif


